package by.dkusch.aircompany.webapp.page.tech.panel;

import javax.inject.Inject;

import org.apache.wicket.Page;
import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.ResourceModel;

import by.dkusch.aircompany.model.tables.PlaneType;
import by.dkusch.aircompany.model.tables.PlaneType_;
import by.dkusch.aircompany.services.PlaneTypeService;
import by.dkusch.aircompany.webapp.page.base.enums.ActionType;
import by.dkusch.aircompany.webapp.page.base.validator.UniqueFieldValidator;

public class PlaneTypeEditPanel extends Panel {

	private static final long serialVersionUID = 1L;

	@Inject
	private PlaneTypeService planeTypeService;

	public PlaneTypeEditPanel(String id, final PlaneType planeType, final PageCreator pageCreator, ActionType action) {
		super(id);

		Form<PlaneType> form = new Form<PlaneType>("form", new CompoundPropertyModel<PlaneType>(planeType));

		final TextField<String> tfName = new TextField<String>("name");
		tfName.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.name"));
		form.add(tfName);

		final TextField<String> tfCl1 = new TextField<String>("passClass1");
		tfCl1.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.pc1.ext"));
		form.add(tfCl1);

		final TextField<String> tfCl2 = new TextField<String>("passClass2");
		tfCl2.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.pc2.ext"));
		form.add(tfCl2);

		final TextField<String> tfCl3 = new TextField<String>("passClass3");
		tfCl3.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.pc3.ext"));
		form.add(tfCl3);

		final TextField<String> tfCrewCount = new TextField<String>("crewCount");
		tfCrewCount.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.crew.count.ext"));
		form.add(tfCrewCount);

		final TextField<String> tfLength = new TextField<String>("length");
		tfLength.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.length"));
		form.add(tfLength);

		final TextField<String> tfWeight = new TextField<String>("weight");
		tfWeight.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.weight"));
		form.add(tfWeight);

		final TextField<String> tfDescription = new TextField<String>("description");
		tfDescription.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.description"));
		form.add(tfDescription);

		final CheckBox chBArc = new CheckBox("arcFl");
		chBArc.add(new PropertyValidator<Boolean>());
		form.add(chBArc);

		form.add(new Label("correctDt"));

		final SubmitLink submitLink = new SubmitLink("sumbit-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onSubmit() {
				super.onSubmit();
				planeTypeService.saveOrUpdate(planeType);

				Page page = pageCreator.createPage();
				page.info(getString("column.common.action.saved"));
				setResponsePage(page);
			}

		};

		submitLink.setOutputMarkupId(true);
		form.add(submitLink);

		final TextField<String> tfCode = new TextField<String>("code");
		tfCode.add(new PropertyValidator<String>()).setLabel(new ResourceModel("plane.type.list.code"));
		tfCode.add(new UniqueFieldValidator<Long, PlaneType>(PlaneType_.code, planeTypeService, planeType)).setLabel(new ResourceModel("plane.type.list.code"));
		form.add(tfCode);

		add(new Link<Void>("back-link") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
			}

		});

		add(form);
		form.setEnabled(action.equals(ActionType.EDIT));

	}

}
